﻿using HuiZe.education.Resource.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace HuiZe.education.View
{
    /// <summary>
    /// CountDown.xaml 的交互逻辑
    /// </summary>
    public partial class CountDown : Window
    {
        /// <summary>
        /// 处理倒计时的委托
        /// </summary>
        /// <returns></returns>
        public delegate bool CountDownHandler();
        private DispatcherTimer timer;

        private ProcessCount processCount;


        private int orderbyType = 0;//0：倒计时  1：正计时
        private bool submitType=false;//是否开起计时
        private CancellationTokenSource cltk;//线程令牌；
        private object obj=new object();
        public CountDown()
        {
            InitializeComponent();
        }
        #region 增加事件
        /// <summary>
        /// 开始计时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void commBorder_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            string minutStr = txtMinutes.Text + txtMinuteg.Text;
            string secondStr = txtSeconds.Text + txtSecondg.Text; //if (orderbyType == 0)//倒序
                if (minutStr=="00"&&secondStr =="00" && orderbyType == 0)
            {
                ShowDialogView showDialogView = new ShowDialogView() { ShowDialogContent = "请先选择计时时间"};
                showDialogView.ShowDialog();
                return;
            }
            if (submitType == false)
            {
                if (timer == null)
                {
                    TimeAutoStart();//加
                }
                SetProcess();

                timer.Start();
                YcbSubmitbtn.Content = "停止计时";
                submitType=true;    
            }
            else
            {
                timer.Stop();
               // cltk.Cancel();//结束
                YcbSubmitbtn.Content = "开始计时";
                submitType = false;
            }
            //if(orderbyType == 0)
            //{

            //}
        }

        private void Image_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
           
        }

        /// <summary>
        /// 处理事件
        /// </summary>
        public event CountDownHandler countDowneven;
        public bool OnCountDown()
        {
            if (countDowneven != null)
                return countDowneven();

            return false;
        }


        private void Minutes1AddBtn_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            int n = 0;
            try
            {
                if (submitType==false)
                {
                    n = Convert.ToInt32(txtMinutes.Text);
                    txtMinutes.Text = addTime(n).ToString();//添加时间
                }
                

            }
            catch (Exception)
            {
                txtMinutes.Text ="0";
            }
        }

        private void Minutes2AddBtn_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            int n = 0;
            try
            {
                if (submitType==false)
                {
                    n = Convert.ToInt32(txtMinuteg.Text);
                    txtMinuteg.Text = addTime(n).ToString(); //添加时间
                }
            }
            catch (Exception)
            {

                throw;
            }

        }
        
        private void Second1AddBtn_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            int n = 0;
            try
            {
                if (submitType==false)
                {
                    n = Convert.ToInt32(txtSeconds.Text);
                    txtSeconds.Text = addMinutegTime(n).ToString(); 
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void Second2AddBtn_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            int n = 0;
            try
            {
                if (submitType==false)
                {
                    n = Convert.ToInt32(txtSecondg.Text);
                    txtSecondg.Text = addTime(n).ToString(); 
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region 方法

        public void SetProcess()
        {
            //转换成秒数
            string minutStr = txtMinutes.Text + txtMinuteg.Text;
            string secondStr = txtSeconds.Text + txtSecondg.Text;

            Int32 minute = Convert.ToInt32(minutStr.Trim());
            Int32 second = Convert.ToInt32(secondStr.Trim());

            //处理倒计时的类
            processCount = new ProcessCount(minute * 60 + second);
            if (orderbyType == 0)//倒序
                countDowneven += new CountDownHandler(processCount.ProcessCountDown);
            else
            {
                countDowneven += new CountDownHandler(processCount.AddCountDown);
            }
        }
        public int addMinutegTime(int num)
        {
            num = num + 1;
            if (num > 5)
            {
                num = 0;
            }
            return num;
        }
        public int delMinutegTime(int num)
        {
            num = num - 1;
            if (num < 0)
            {
                return 5;
            }
            return num;
        }
        public int addTime(int num)
        {
            num = num + 1;
            if (num > 9)
            {
                num = 0;
            }
            return num;
        }
        public int delTime(int num)
        {
            num = num - 1;
            if (num < 0)
            {
                return 9;
            }
            return num;
        }

        public void Initialize()
        {
            txtMinutes.Text = "0";//分十位
            txtMinuteg.Text = "0";//分个位
            txtSecondg.Text = "0";//秒十位
            txtSeconds.Text = "0"; //秒个位
        }

        public void TimeAutoStart()
        {
            if (timer == null)
            {
                timer = new DispatcherTimer();
            }
            timer.Interval = new TimeSpan(10000000);   //时间间隔为一秒
            timer.Tick += new EventHandler(timer_Tick);
        }
        /// <summary>
        /// Timer触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            if (OnCountDown())
            {
                txtMinutes.Text = processCount.GetMinute().Substring(0, 1);
                txtMinuteg.Text = processCount.GetMinute().Substring(1, 1);
                txtSeconds.Text = processCount.GetSecond().Substring(0, 1);
                txtSecondg.Text = processCount.GetSecond().Substring(1, 1);
            }
            else
                timer.Stop();
        }
        #endregion

        #region 减少事件


        private void Minutes1Reduce_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            int n = 0;
            try
            {
                if (submitType==false)
                {
                    n = Convert.ToInt32(txtMinutes.Text);
                    txtMinutes.Text = delTime(n).ToString();
                }

            }
            catch (Exception)
            {

                throw;
            }
        }



        private void Minutes2Reduce_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            int n = 0;
            if (submitType==false)
            {
                n = Convert.ToInt32(txtMinuteg.Text);
                txtMinuteg.Text = delTime(n).ToString(); 
            }
        }

        private void Second1Reduce_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            int n = 0;
            if(submitType==false)
            {
                n = Convert.ToInt32(txtSeconds.Text);
                txtSeconds.Text = delMinutegTime(n).ToString();
            }

        }

        private void Second2Reduce_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            int n = 0;
            if(submitType == false)
            {
                n = Convert.ToInt32(txtSecondg.Text);
                txtSecondg.Text = delTime(n).ToString();
            }
        }
        #endregion

        #region 切换事件

        private void Grid_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            orderbyType = 0;
            if (timer != null)
            {
                timer.Stop();
                YcbSubmitbtn.Content = "开始计时";
                submitType = false;

            }
            Initialize();//初始化值 
            descBtn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#55e4c2"));
            aseBtn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffffff"));
            txtCountDown.Foreground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffffff"));
            txtPositiveTiming.Foreground=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#000000"));
        }

        private void Grid_PreviewMouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
        {
            orderbyType = 1;
            if(timer != null)
            {
                timer.Stop();
                YcbSubmitbtn.Content = "开始计时";
                submitType = false;
            }
            Initialize();//初始化值 
            descBtn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffffff"));
            aseBtn.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#55e4c2"));
            txtCountDown.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#000000"));
            txtPositiveTiming.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffffff"));
        }

        #endregion

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}
